clear all 

*** Dataset: 
use 	"$data\2_firm_ano_perfvars_individual_paper.dta", clear
keep 	if wave==1

*** Define variables by section
local outcomevars sh_aminwage sh_wrcon sh_sosec empquality revenue_wins5_base profit_wins5_base prod_labour_wins5_base costs_labour_wins5_base

local stratavars abidjan revenue_wins5_base sized1 sized2 sized3 firmsize empsized1 empsized2 empsized3 female_share 

local balvars broadsecd1 broadsecd2 broadsecd3 broadsecd4 firmage m_capital registered_commerce gender_dirigeant educ_diri_tert

*** Prepare labels
* Outcomes
label var empquality "Formality Index"
label var sh_aminwage "Minimum wage (share)"
label var sh_wrcon "Written contract (share)"
label var sh_sosec "Social security (share)"
la var revenue_wins5_base	"An. revenue (18–20, TH.USD)" 
la var profit_wins5_base	"An. profit (18–20, TH.USD)"
la var prod_labour_wins5_base		"Labor prod."
la var costs_labour_wins5_base		"An. labor costs (18–20, TH.USD)"
		
* Strata vars
label var abidjan "Abidjan"
label var revenue_wins5_base "Revenue (18–20, TH.USD)"
label var sized1 "Size: micro"
label var sized2 "Size: small"
label var sized3 "Size: medium"
label var firmsize "Staff (18-20)"
label var empsized1 "1-3 staff"
label var empsized2 "4-6 staff"
label var empsized3 "More than 6 staff"
label var female_share "Share of female staff"

* Firm characteristics
la var broadsecd1 "Act: Manufacturing \& Agriculture"
la var broadsecd2 "Act: Construction"
la var broadsecd3 "Act: Wholesale and Retail"
la var broadsecd4 "Act: Other Services"
la var firmage "Firm age (years)"
la var m_capital "Capital stock (18–20, TH.USD)"
la var registered_commerce "Registry of commerce"	

*** Create LaTeX table directly
file open table using "$results\01_tables\Table_S6_balance_table_firm.tex", write replace
file write table "\begin{tabular}{lllllllllllll} \hline \hline" _n
file write table "                             & \multicolumn{2}{l}{\textbf{Treatment}} & \multicolumn{2}{l}{\textbf{Control}} & \multicolumn{2}{l}{\textbf{Orthogonality}} & \multicolumn{2}{l}{\textbf{Take up}} & \multicolumn{2}{l}{\textbf{No take up}} & \multicolumn{2}{l}{\textbf{Orthogonality}} \\" _n
file write table "                             & \multicolumn{2}{l}{(1)}       & \multicolumn{2}{l}{(2)}     & \multicolumn{2}{l}{\textbf{Mean (1)-(2)}}  & \multicolumn{2}{l}{(3)}     & \multicolumn{2}{l}{(4)}             & \multicolumn{2}{l}{\textbf{Mean (3)-(4)}}  \\" _n
file write table "                             & \textbf{N}            & \textbf{Mean}           & \textbf{N}           & \textbf{Mean}          & \textbf{Difference}        & \textbf{p-value}       & \textbf{N}           & \textbf{Mean}          & \textbf{N}               & \textbf{Mean}              & \textbf{Difference}        & \textbf{p-value}       \\ \hline \\" _n

* Loop through each section
foreach section in outcomevars stratavars balvars {
    if "`section'"=="outcomevars" {
        file write table "\textbf{Outcome variables}   &                &                 &              &                &                    &                &              &                &                    &                    &                    &                \\ " _n
    }
    else if "`section'"=="stratavars" {
        file write table "\\ \hline \\" _n
        file write table "\textbf{Strata variables}    &                &                 &              &                &                    &                &              &                &                    &                    &                    &                \\" _n
    }
    else {
        file write table "\\ \hline \\" _n
        file write table "\textbf{Firm characteristics} &                &                 &              &                &                    &                &              &                &                    &                    &                    &                \\ " _n
    }
    
    foreach var of varlist ``section'' {
        * Treatment vs Control comparison
        qui mean `var', over(assignment)
        local nt = e(_N)[1,2]
        local nc = e(_N)[1,1]
        local mt : display %9.3f e(b)[1,2]
        local mc : display %9.3f e(b)[1,1]
        local diff : display %9.3f e(b)[1,2]-e(b)[1,1]
        qui ttest `var', by(assignment)
        local p : display %5.3f r(p)
        
        * Take-up vs No take-up comparison (among treatment group)
        qui mean `var' if assignment==1, over(morethan1visit)
        local ntu = e(_N)[1,2]
        local ntu0 = e(_N)[1,1]
        local mtu : display %9.3f e(b)[1,2]
        local mtu0 : display %9.3f e(b)[1,1]
        local difftu : display %9.3f e(b)[1,2]-e(b)[1,1]
        qui ttest `var' if assignment==1, by(morethan1visit)
        local ptu : display %5.3f r(p)
        
        local label : variable label `var'
        file write table "`label' &         `nt'&       `mt'&         `nc'&       `mc'&       `diff'&        `p'&         `ntu'&       `mtu'&         `ntu0'&       `mtu0'&       `difftu'&        `ptu'\\" _n
    }
}

file write table "\hline" _n
file write table "\end{tabular}" _n
file close table



clear all 

*** Dataset: 
use 	"$data\2_firm_ano_perfvars_individual_paper.dta", clear
keep 	if wave==1

*** Define variables by section
local outcomevars sh_aminwage sh_wrcon sh_sosec empquality revenue_wins5_base profit_wins5_base prod_labour_wins5_base costs_labour_wins5_base

local stratavars abidjan revenue_wins5_base sized1 sized2 sized3 firmsize empsized1 empsized2 empsized3 female_share 

local balvars broadsecd1 broadsecd2 broadsecd3 broadsecd4 firmage m_capital registered_commerce gender_dirigeant educ_diri_tert

*** Prepare labels
* Outcomes
label var empquality "Formality Index"
label var sh_aminwage "Minimum wage (share)"
label var sh_wrcon "Written contract (share)"
label var sh_sosec "Social security (share)"
la var revenue_wins5_base "An. revenue (18–20, TH.USD)" 
la var profit_wins5_base "An. profit (18–20, TH.USD)"
la var prod_labour_wins5_base "Labor prod."
la var costs_labour_wins5_base "An. labor costs (18–20, TH.USD)"
		
* Strata vars
label var abidjan "Abidjan"
label var revenue_wins5_base "Revenue (18–20, TH.USD)"
label var sized1 "Size: micro"
label var sized2 "Size: small"
label var sized3 "Size: medium"
label var firmsize "Staff (18-20)"
label var empsized1 "1-3 staff"
label var empsized2 "4-6 staff"
label var empsized3 "More than 6 staff"
label var female_share "Share of female staff"

* Firm characteristics
la var broadsecd1 "Act: Manufacturing \& Agriculture"
la var broadsecd2 "Act: Construction"
la var broadsecd3 "Act: Wholesale and Retail"
la var broadsecd4 "Act: Other Services"
la var firmage "Firm age (years)"
la var m_capital "Capital stock (18–20, TH.USD)"
la var registered_commerce "Registry of commerce"	
la var gender_dirigeant "Male manager"
la var educ_diri_tert "Manager w/tertiary education"

*** Create LaTeX table directly
file open table using "$results\01_tables\Table_S7_balance_table_firm_endline.tex", write replace
file write table "\begin{tabular}{lllllllllllll} \hline \hline" _n
file write table "                             & \multicolumn{2}{l}{\textbf{Observed}} & \multicolumn{2}{l}{\textbf{Drop-out}} & \multicolumn{2}{l}{\textbf{Difference}} & \multicolumn{2}{l}{\textbf{Treatment}} & \multicolumn{2}{l}{\textbf{Control}} & \multicolumn{2}{l}{\textbf{Difference}} \\" _n
file write table "                             & \multicolumn{2}{l}{(1)}       & \multicolumn{2}{l}{(2)}     & \multicolumn{2}{l}{\textbf{Mean (1)-(2)}}  & \multicolumn{2}{l}{(3)}     & \multicolumn{2}{l}{(4)}             & \multicolumn{2}{l}{\textbf{Mean (3)-(4)}}  \\" _n
file write table "                             & \textbf{N}            & \textbf{Mean}           & \textbf{N}           & \textbf{Mean}          & \textbf{Difference}        & \textbf{p-value}       & \textbf{N}           & \textbf{Mean}          & \textbf{N}               & \textbf{Mean}              & \textbf{Difference}        & \textbf{p-value}       \\ \hline \\" _n

* Loop through each section
foreach section in outcomevars stratavars balvars {
    if "`section'"=="outcomevars" {
        file write table "\textbf{Outcome variables}   &                &                 &              &                &                    &                &              &                &                    &                    &                    &                \\ " _n
    }
    else if "`section'"=="stratavars" {
        file write table "\\ \hline \\" _n
        file write table "\textbf{Strata variables}    &                &                 &              &                &                    &                &              &                &                    &                    &                    &                \\" _n
    }
    else {
        file write table "\\ \hline \\" _n
        file write table "\textbf{Firm characteristics} &                &                 &              &                &                    &                &              &                &                    &                    &                    &                \\ " _n
    }
    
    foreach var of varlist ``section'' {
        * Panel vs Dropout comparison
        qui mean `var', over(panel_2)
        local nt = e(_N)[1,2]
        local nc = e(_N)[1,1]
        local mt : display %9.3f e(b)[1,2]
        local mc : display %9.3f e(b)[1,1]
        local diff : display %9.3f e(b)[1,2]-e(b)[1,1]
        qui ttest `var', by(panel_2)
        local p : display %5.3f r(p)
        
        * Treatment vs Control comparison (among panel firms)
        qui mean `var' if panel_2==1, over(assignment)
        local ntu = e(_N)[1,2]
        local ntu0 = e(_N)[1,1]
        local mtu : display %9.3f e(b)[1,2]
        local mtu0 : display %9.3f e(b)[1,1]
        local difftu : display %9.3f e(b)[1,2]-e(b)[1,1]
        qui ttest `var' if panel_2==1, by(assignment)
        local ptu : display %5.3f r(p)
        
        local label : variable label `var'
        file write table "`label' &         `nt'&       `mt'&         `nc'&       `mc'&       `diff'&        `p'&         `ntu'&       `mtu'&         `ntu0'&       `mtu0'&       `difftu'&        `ptu'\\" _n
    }
}

file write table "\hline" _n
file write table "\end{tabular}" _n
file close table